import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ajax from '@/api/index'
import VueWechatTitle from 'vue-wechat-title'
import Vant from 'vant'
import 'vant/lib/index.css'
import Vconsole from 'vconsole'

// let vConsole = new Vconsole()
// Vue.use(vConsole)


Vue.use(VueWechatTitle)
Vue.use(Vant)
// 移动端适配
import 'amfe-flexible/index'
// 设置为 false 以阻止 vue 在启动时生成生产提示
Vue.config.productionTip = false
Vue.prototype.$http = ajax

// router.beforeEach(async (to, from, next) => {

//     next()
// })


// 获取code
Vue.prototype.getCode = function(path) {
  const code = GetUrlParam()
  let redirect_url = encodeURIComponent(process.env.VUE_APP_WEB_URL + path)
  if (!code.length) {
    window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb4d8eb0bd9fb657c&redirect_uri=${redirect_url}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`
  }
  return code
}

Vue.prototype.redirectGetCode = function(path) {
  let redirect_url = encodeURIComponent(process.env.VUE_APP_WEB_URL + path)
  window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb4d8eb0bd9fb657c&redirect_uri=${redirect_url}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`
  return GetUrlParam()
}

function GetUrlParam() {
  let url = window.location.href
  if( url.indexOf("?") != -1 ) {
    let str = url.substring(url.indexOf('?')+1)
    let arr = str.split('&')
    let code = ''
    for (let i = 0; i < arr.length; i++) {
        if (arr[i].indexOf('code') != -1) {
          code = arr[i].split('=')[1]
        }
    }
    return code.length ? code : ''
  } else {
    return ''
  }
}

// 应用启动时恢复登录数据
store.dispatch('restoreLoginData')

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
